<style >
	@import url("../../common/css/my.css");
</style>
<template>
	<view>
		<view class="user_top" 
		:style="[is_vip_user ? { height:'476rpx'} : '']"
		>
			<image class="user_top_bg" src="../../static/my_bg.png" mode="aspectFill" ></image>
			<avatar :size="100" :src="userAvatar" :marginTop="96" :marginLeft="40" style="float: left;"></avatar>
			<text class="user_top_text" v-if="userName != ''" >{{userName}}</text>
			<text class="user_top_text" v-else @click="getUserInfo">请点击获取授权</text>
			<view class="myVipCard" v-if="is_vip_user">
				<view class="myVipCardInfo">
					<image class="myVipCardInfo-bg" src="../../static/my_vip_bg.png" mode="aspectFill"></image>
					<view class="myVipCardContent">
						<view class="myVipCardLeft">
							<view class="card_name">跑跑会员卡</view>
							<view class="card_times">
								<text class="card_times_text">还剩余</text>
								<text class="card_times_num">{{userVipInfo.usageTimes}}</text>
								<text class="card_times_text">次</text>
							</view>
						</view>
						<view class="myVipCardRight">
							<!-- <view class="availabelTimes">可用次数25次</view> -->
							<!-- <view class="renewal_btn">立即续费</view> -->
							<text class="dueDate">最近一次购买时间</text>
							<text class="dueDate">{{userVipInfo.time}}</text>
						</view>
					</view>
				</view>
				<view class="myVipCardBottom">
					<image class="myVipCardInfo-bottom" src="../../static/my_vip_bottom.png" mode="aspectFill"></image>
				</view>
			</view>
		</view>
		<view class="vipBuy" 
		:style="[is_vip_user ? '' : {marginTop:'-140rpx',backgroundColor:'#FFFFFF'} ]"
		>
			<view class="vipBuy_top">
				<text class="vipBuy_top_title">会员购买</text>
				<text class="vipBuy_top_time">每月初更新次数</text>
			</view>
			<view class="vipTimeList">
				<vipSwiper :datas="vipGoodsList" :config="{circular:true}" :disable="true" @chooseVipSelect="chooseVipSelect"></vipSwiper>
			</view>
		</view>
		<view class="my_setting">
			<view class="setting_item" @click="goToAddress">
				<image class="setting_img_left" src="../../static/position.png" mode=""></image>
				<text class="setting_title">常用地址</text>
				<image class="setting_img_right" src="../../static/9a_right.png" mode=""></image>
			</view>
			<view class="setting_item" @click="tel">
				<image class="setting_img_left" src="../../static/kf.png" mode=""></image>
				<text class="setting_title">客服电话</text>
				<image class="setting_img_right" src="../../static/9a_right.png" mode=""></image>
			</view>
			<view class="setting_item" @click="goToSetting">
				<image class="setting_img_left" src="../../static/setting.png" mode=""></image>
				<text class="setting_title">设置</text>
				<image class="setting_img_right" src="../../static/9a_right.png" mode=""></image>
			</view>
		</view>
		<!-- <view class="applyVip" v-if="!is_vip" @click="applyVip">
			申请代理
		</view> -->
		<view class="loginVip" v-if="is_vip == 1"  @click="loginVip">
			登录代理后台
		</view>
		<view class="applyVip" v-if="is_vip == 2" @click="goAdminHome">
			进入后台
		</view>
		<!--登录提示魔窗-->
		<!-- <showToast @cancel="cancel" v-show="showLoginFlag"></showToast> -->
		<!--登录弹窗-->
		<showSignIn @cancelLogin="cancelLogin" @loginSubmit="loginSubmit" v-show="showLoginSuFlag"></showSignIn>
		<!--底部导航-->
		<tabBar :pagePath="'/pages/my/index'"></tabBar>
	</view>
</template>

<script>
	import avatar from '@/components/user/avatar.vue'
	import vipSwiper from '@/components/user/vipswiper.vue'
	import showToast from '@/components/show-model/show-toast/show-toast.vue'
	import showSignIn from '@/components/show-model/show-signIn/show-signIn.vue'

	export default{
		components:{
			avatar,vipSwiper,showToast,showSignIn
		},
		data(){
			return {
				vipGoodsList:[],
				is_vip:0, //1登录后台 2进入后台
				showLoginFlag:false,//后台未登录状态
				showLoginSuFlag:false,//后台已验证登录状态
				userAvatar:'',
				userName:'',
				userVipInfo:{},
				is_vip_user:0,//是否开通跑腿会员
				kf_phone:''
			}
		},
		onLoad(){
			let that = this
			this.initVipBuyTimeSetting()
			this.vipInfo()
			this.getKefuPhone()
			let adminType = uni.getStorageSync('adminType')
			let adminAgent = uni.getStorageSync('adminIsAgent') // true 需验证 false 无需要验证
			if(adminType == 1){
				that.is_vip = 1;
			}else{
				that.is_vip = 2;
			}
			
			//检测是否本地授权过用户信息
			if(uni.getStorageSync('userAvatar')){
				that.userAvatar = uni.getStorageSync('userAvatar')
				that.userName   = uni.getStorageSync('userName')
			}
		},
		methods:{
			//加载套餐列表
			initVipBuyTimeSetting(){
				let that = this
				this.$api.vip.vipList()
				.then(res=>{
					if(res.code == 200){
						that.vipGoodsList = res.data
					}else{
						this.$commonJs.showToast.showNoneToast(res.msg)
					}
				})
				.catch(err => {
					this.$commonJs.showToast.showNoneToast(err)
				})
			},
			getKefuPhone(){
				let that = this
				this.$api.adminStore.getKePhone(this.$store.state.txMap.scsId)
				.then(res => {
					if(res.code == 200){
						 that.kf_phone = res.data
					}
				})
			},
			vipInfo(){
				let that = this
				this.$api.vip.getVipInfo()
				.then(res => {
					console.log(res)
					if(res.code == 200){
						if(res.data.id != -1){
							that.is_vip_user = 1
						}else{
							that.is_vip_user = 0 
						}
						that.userVipInfo = res.data
					}else{
						this.$commonJs.showToast.showNoneToast(res.msg)
					}
				})
				.catch(err => {
					this.$commonJs.showToast.showNoneToast('会员卡信息有误，请联系工作人员')
				})
			},
			//选择套餐
			chooseVipSelect(e){
				console.log(e)
			},
			//进入后台
			goAdminHome(){
				let adminAgent = this.checkLoginFlag()
				if(adminAgent){
					//信息采集
					this.$uniNavigateTo('../../pages/my/applyVip/applyVip/applyVip');
				}else{
					uni.reLaunch({
						url:"../../pages/admin/home/home"
					})
				}
			},
			checkLoginFlag(){
				let adminAgent = uni.getStorageSync('adminIsAgent')
				return adminAgent
			},
			//地址列表
			goToAddress(){
				this.$uniNavigateTo('../../pages/address/address-list/address-list');
			},
			//设置密码
			goToSetting(){
				this.$uniNavigateTo('../../pages/my/setting/setting');
			},
			//悬挂按钮点击
			loginVip(){
				let that = this
				that.showLoginSuFlag = true
			},
			//关闭登陆提示弹窗
			cancel(){
				this.showLoginFlag = false
			},
			//关闭登录界面
			cancelLogin(){
				this.showLoginSuFlag = false
			},
			//拨打客服电话
			tel(){
				let that = this
				uni.makePhoneCall({
				    phoneNumber: that.kf_phone
				});
			},
			//登录
			loginSubmit(e){
				console.log(e)
				let postData 	=	{"username":e.account,"password":e.pwd,"code":e.code}
				this.$api.adminAct.adminLogin(postData)
				.then(res=>{
					if(res.code == 200){
						this.$store.dispatch('setAdminInfo',res.data)
						console.log(res.data)
						if((res.data.type == 4 || res.data.type == 3) && res.data.agentPageShow){
							this.$uniNavigateTo('../../pages/my/applyVip/applyVip/applyVip');
						}else{
							this.$commonJs.waitFunction.waitRelaunch(res.msg,'../../pages/admin/home/home')
						}
						
					}
				})
				.catch(err=>{
					this.$commonJs.showToast.showNoneToast(err)
				})
			},
			getUserInfo(){
				let that = this
				uni.getUserProfile({
					desc: '授权头像',
					success: function(infoRes) {
						that.userAvatar = infoRes.userInfo.avatarUrl
						that.userName = infoRes.userInfo.nickName
						uni.setStorageSync('userAvatar',infoRes.userInfo.avatarUrl)
						uni.setStorageSync('userName',infoRes.userInfo.nickName)
					}
				});
			}
		}
	}
</script>

